package eu.boss.activities;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import eu.boss.R;
import eu.boss.model.Player;

/**
 * This activity is displaying to add a new player.
 * 
 * @author Arnaud
 * 
 */

public class NewPlayerActivity extends Activity {

	public static String BLEU = "Bleu";
	public static String ROUGE = "Rouge";
	public static String NOIR = "Noir";
	public static String VERT = "Vert";
	public static String JAUNE = "Jaune";
	public static String BLANC = "Blanc";
	public static String GRIS = "Gris";
	public static String ORANGE = "Orange";
	public static String SABLE = "Sable";
	public static String ROSE = "Rose";
	public static String VIOLET = "Violet";
	
	private List<String> colorList;
	private int colorSelected = 0;
	private Player p;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newplayer);

		// initialize components
		createColorList();
		final EditText edtNewPlayer = (EditText) findViewById(R.id.etNewPlayerName);
		Spinner spinner = (Spinner) findViewById(R.id.spinnerColor);
		Button btnConfirm = (Button) findViewById(R.id.btnConfirm);

		// spinner adapter which will add datas in the spinner
		ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, android.R.id.text1);
		spinnerAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(spinnerAdapter);

		// adding datas in the adapter
		for (int i = 0; i < colorList.size(); i++) {
			spinnerAdapter.add(colorList.get(i));
		}
		// update UI
		spinnerAdapter.notifyDataSetChanged();

		// putting a listener on the two buttons
		btnConfirm.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (v.getId() == R.id.btnConfirm) {
					if (edtNewPlayer.getText().toString().compareTo("") != 0) {
						p = new Player(edtNewPlayer.getText().toString(),
								colorSelected, 0, 0, 0, 0, 0, 0);
						try {

							ObjectMapper mapper = new ObjectMapper();
							String strPlayer;
							strPlayer = mapper.writeValueAsString(p);
							Intent intent = new Intent();
							intent.putExtra("player", strPlayer);
							setResult(RESULT_OK, intent);
							finish();
						} catch (JsonGenerationException e) {
							e.printStackTrace();
						} catch (JsonMappingException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						} catch (Exception e) {
							e.printStackTrace();
						}
					} else
					// nothing in the editText so display an error message
					{
						AlertDialog.Builder adb = new AlertDialog.Builder(
								NewPlayerActivity.this);
						adb.setTitle("Erreur");
						adb.setMessage("Veuillez entrer un nom svp");
						adb.setPositiveButton("Ok", null);
						adb.show();
					}
				}
			}
		});

		// putting a listener on the spinner. The color is changed with the
		// selected item
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View v,
					int position, long id) {
				switch (position) {
				case 0:
					colorSelected = R.drawable.rounded_white;
					break;
				case 1:
					colorSelected = R.drawable.rounded_blue;
					break;
				case 2:
					colorSelected = R.drawable.rounded_gray;
					break;
				case 3:
					colorSelected = R.drawable.rounded_yellow;
					break;
				case 4:
					colorSelected = R.drawable.rounded_black;
					break;
				case 5:
					colorSelected = R.drawable.rounded_orange;
					break;
				case 6:
					colorSelected = R.drawable.rounded_pink;
					break;
				case 7:
					colorSelected = R.drawable.rounded_red;
					break;
				case 8:
					colorSelected = R.drawable.rounded_sand;
					break;
				case 9:
					colorSelected = R.drawable.rounded_green;
					break;
				case 10:
					colorSelected = R.drawable.rounded_purple;
					break;
				default:
					break;
				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
			// Do nothing
		});
	}

	/**
	 * Creation of the colorList
	 */
	public void createColorList() {
		colorList = new ArrayList<String>();
		colorList.add(BLANC);
		colorList.add(BLEU);
		colorList.add(GRIS);
		colorList.add(JAUNE);
		colorList.add(NOIR);
		colorList.add(ORANGE);
		colorList.add(ROSE);
		colorList.add(ROUGE);
		colorList.add(SABLE);
		colorList.add(VERT);
		colorList.add(VIOLET);
		
	}

}
